package com.jwthhealth_pub.care;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.app.view.MainActivity;
import com.jwthhealth_pub.care.adapter.CareMsgAdapter;
import com.jwthhealth_pub.common.JwthImgLoader;
import com.jwthhealth_pub.common.base.BasetFragment;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.home.presenter.HealthPresenter;
import com.jwthhealth_pub.home.view.HomeFrgment;
import com.jwthhealth_pub.old.acupressure.view.AcupressureActivity;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.diet.view.DietActivity;
import com.jwthhealth_pub.old.sign.view.SignInActivity;
import com.jwthhealth_pub.old.sportfitness.view.SportFitnessHomeActivity;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.jwthhealth_pub.app.App.mContext;
import static com.jwthhealth_pub.common.LogUtil.makeLogTag;


/**
 * 保健
 * Created by yang on 2017/12/13.
 */

public class CareFragment extends BasetFragment implements HealthPresenter.View {
    private static final String banner_img_one = "http://192.168.1.108//uploads/20171012/dXNlclVwbG9hZDE1MDc3OTE2NDE4ODcyMzMxNTE0.jpg";
    private static final String banner_img_two = "http://192.168.1.108//uploads/20170829/dXNlclVwbG9hZDE1MDM5OTY4Njk5MDgwNDg1ODM4.jpg";
    private static final String banner_img_thi = "http://192.168.1.108//uploads/20171013/dXNlclVwbG9hZDE1MDc4NzUxNDUzNDA4NzE5NjE5.jpg";
    private static final String banner_img_for = "http://192.168.1.108//uploads/20171013/dXNlclVwbG9hZDE1MDc4NzUzOTI2NjE0NjM1MjE=.jpg";
    private static final String banner_img_fiv = "http://192.168.1.108//uploads/20171013/dXNlclVwbG9hZDE1MDc4NzQwOTA4NTUxODM0OTc5.jpg";
    private static final String TAG = makeLogTag(HomeFrgment.class);
    @BindView(R.id.banner_home)
    Banner bannerHome;
    @BindView(R.id.rv_home_msg)
    RecyclerView rvMsg;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.fragment_care, null);
        unbinder = ButterKnife.bind(this, view);
        showBanner();
        initRecyclerView();
        return view;
    }

    //这是轮播
    public void showBanner() {
        // TODO: 2017/12/5 根据屏幕的大小调整banner的尺寸
        List<Object> images = new ArrayList<>();
        images.add(banner_img_one);
        images.add(banner_img_two);
        images.add(banner_img_thi);
        images.add(banner_img_for);
        images.add(banner_img_fiv);

        bannerHome.setDelayTime(3000);
        bannerHome.setIndicatorGravity(Gravity.BOTTOM);

        bannerHome.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.d(TAG, "onPageScrolled position:" + position);
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        OnBannerClickListener listener = new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), "click", Toast.LENGTH_SHORT).show();
            }
        };

        bannerHome.setImages(images)
                .setImageLoader(new BannderImgLoader())
                .setOnBannerClickListener(listener)
                .start();
    }

    //添加RecyclerView
    public void initRecyclerView() {
        rvMsg.setLayoutManager(new LinearLayoutManager(getActivity()));
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rvMsg.setHasFixedSize(true);
        rvMsg.setAdapter(new CareMsgAdapter((MainActivity) getActivity()));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_bao_yingyang, R.id.btn_bao_sport, R.id.btn_bao_lib})
    public void onViewClicked(View view) {
        try {
            switch (view.getId()) {
                case R.id.btn_bao_yingyang:
                    if (App.preferenceUtil.getBoolean(PreferenceKey.CACHE_TOKEN, false)) {
                        Intent intent =   new Intent(mContext, DietActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else {
                        Toast.makeText(mContext, mContext.getResources().getString(R.string.diet_sign), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, SignInActivity.class);
                        intent.putExtra(Constant.SIGN_JUMP_TOKEN, SignInActivity.FROM_HOME);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                    break;
                case R.id.btn_bao_sport:
                    if (App.preferenceUtil.getBoolean(PreferenceKey.CACHE_TOKEN, false)) {
                        Intent intent =   new Intent(mContext, SportFitnessHomeActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else {
                        Toast.makeText(mContext, mContext.getResources().getString(R.string.diet_sign), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, SignInActivity.class);
                        intent.putExtra(Constant.SIGN_JUMP_TOKEN, SignInActivity.FROM_HOME);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                    break;
                case R.id.btn_bao_lib:

                    if (App.preferenceUtil.getBoolean(PreferenceKey.CACHE_TOKEN, false)) {
                        Intent intent = new Intent(mContext, AcupressureActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    } else {
                        Toast.makeText(mContext, mContext.getResources().getString(R.string.diet_sign), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(mContext, SignInActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra(Constant.SIGN_JUMP_TOKEN, SignInActivity.FROM_HOME);
                        mContext.startActivity(intent);
                    }

                    break;
            }
        }catch (Exception e){
          Log.i("tag",e.getMessage());
        }
    }

    @Override
    protected void title() {

    }

    private class BannderImgLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            String imgPath = String.valueOf(path);
//            Picasso.with(context).load(imgPath).into(imageView);
            Log.d("MyImageLoader", imgPath);
            JwthImgLoader.picasso(imgPath, imageView);
        }
    }

}